﻿using Loki.Data;

namespace Loki.Model
{
    public abstract class AtomicEntity<TData, TKey> : Entity, IAtomicEntity<TKey, TData>
        where TData : class, IDataEntity<TKey>
    {
        protected AtomicEntity()
            : base()
        {
            Data = Context.Get<TData>();
            Data.AlreadyPersisted = false;
        }

        protected AtomicEntity(IAtomicEntity<TKey, TData> clone)
            : base(clone)
        {
            BeginInit();
            Data = Context.Get<TData>();
            ID = clone.ID;
            Data.AlreadyPersisted = clone.Data.AlreadyPersisted;
            EndInit(!IsChanged);
        }

        protected AtomicEntity(TData clone)
            : base()
        {
            Data = clone;
        }

        public TData Data
        {
            get;
            private set;
        }

        #region ID

        public TKey ID
        {
            get
            {
                return Data.PK;
            }

            private set
            {
                Data.PK = value;
            }
        }

        #endregion ID
    }
}